package com.myopen.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.jfinal.core.ActionKey;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.myopen.base.BaseController;
import com.myopen.base.BaseDataTableVo;
import com.myopen.base.BaseVo;
import com.myopen.model.WebsiteColumn;
import com.myopen.model.WebsiteContent;

public class WebsiteContentController extends BaseController{

	@ActionKey("/admin/websiteContent")
	public void index() {
		render("/admin/websiteContent/index.html");
	}
	
	@ActionKey("/admin/websiteContent/pagequery")
	public void websiteContentPageQuery() {
		
		Record record=getSearchRecord();//会自动组装通用的参数
		record.set("searchContent", getPara("searchContent"));
		record.set("status", getPara("status"));
		record.set("module", getPara("module"));
		Page<Record> page=WebsiteContent.dao.pagequery(record);
		
		BaseDataTableVo tableVo=new BaseDataTableVo(page);
		renderJson(tableVo);
	}
	
	@ActionKey("/admin/websiteContent/recycle")
	public void websiteContentRecycle() {
		render("/admin/websiteContent/index_recycle.html");
	}
	
	@ActionKey("/admin/websiteContent/recycle/pagequery")
	public void websiteContentPageRecycleQuery() {
		
		Record record=getSearchRecord();//会自动组装通用的参数
		record.set("searchContent", getPara("searchContent"));
		record.set("status", getPara("status"));
		record.set("removed", "1");
		record.set("module", getPara("module"));
		Page<Record> page=WebsiteContent.dao.pagequery(record);
		
		BaseDataTableVo tableVo=new BaseDataTableVo(page);
		renderJson(tableVo);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ActionKey("/admin/websiteContent/publish")
	public void websiteContentPublish(){
		
		//Record currUser=getAdminUser();
		Record websiteContent=new Record();
		Map<String,Object> attribute_map=new HashMap();
		String websiteColumnId=getPara("websiteColumnId");
		String websiteContentId=getPara("websiteContentId");
		
		if(!StrKit.isBlank(websiteContentId)) {
			websiteContent=WebsiteContent.dao.getRecordById(websiteContentId);
			attribute_map=websiteContent.get("attribute_map");
		}
		
		setAttr("websiteColumnId", websiteColumnId);
		setAttr("vo", websiteContent);
		setAttr("attribute_map", attribute_map);

		render("/admin/websiteContent/publish.html");
	}
	
	@ActionKey("/admin/websiteContent/publish/save")
	public void websiteContentPublishSave(){
		
		BaseVo baseVo=new BaseVo();
		
		Record currUser=getAdminUser();
		
		String websiteColumnId=getPara("websiteColumnId");
		String title=getPara("title");
		String id=getPara("id");
		Date publishTime=getParaToDate("publishTime");
		String attribute=getPara("attribute");
		
		WebsiteContent vo=new WebsiteContent();
		vo.set("websiteColumnId", websiteColumnId);
		vo.set("title", title);
		vo.set("publishTime", publishTime);
		vo.set("id", id);
		vo.set("attribute", attribute);
		
		WebsiteContent.dao.save(vo, currUser);
		
		baseVo.setCode("0");
		
		renderJson(baseVo);
	}
	
	@ActionKey("/admin/websiteContent/startUsing")
	public void startUsing(){
		
		Record currUser=getAdminUser();
		String id=getPara("id");
		WebsiteContent.dao.changeStatus(id, "CMS_CONTENT_STATUS_00", currUser);
		
		BaseVo baseVo=new BaseVo();
		renderJson(baseVo);
		
	}
	
	@ActionKey("/admin/websiteContent/forbidden")
	public void forbidden(){
		
		Record currUser=getAdminUser();
		String id=getPara("id");
		WebsiteContent.dao.changeStatus(id, "CMS_CONTENT_STATUS_01", currUser);
		
		BaseVo baseVo=new BaseVo();
		renderJson(baseVo);
		
	}
	
	@ActionKey("/admin/websiteContent/delete")
	public void delete(){
		
		Record currUser=getAdminUser();
		String id=getPara("id");
		WebsiteContent.dao.remove(id,currUser);
		
		BaseVo baseVo=new BaseVo();
		renderJson(baseVo);
		
	}
	
	@ActionKey("/admin/websiteContent/recover")
	public void recover(){
		
		Record currUser=getAdminUser();
		String id=getPara("id");
		WebsiteContent.dao.recover(id,currUser);
		
		BaseVo baseVo=new BaseVo();
		renderJson(baseVo);
		
	}
	
	
}
